import re
import yaml
import math
import os
import curses


class Tools:
    remove_br = re.compile('<br>')
    remove_div = re.compile('<div.*?>|</div>')
    remove_img = re.compile('<img.*?>')

    remove_blank = re.compile('\s')
    remove_a = re.compile('<a.*?>|</a>')
    remove_nbsp = re.compile('&nbsp;')

    def replace(self, x):
        x = re.sub(self.remove_br, '', x)
        x = re.sub(self.remove_div, '', x)
        x = re.sub(self.remove_img, '', x)
        x = re.sub(self.remove_a, '', x)
        x = re.sub(self.remove_nbsp, '', x)

        return x.strip()

    def replace_blank(self, x):
        x = re.sub(self.remove_blank, '', x)

        return x.strip()

    @staticmethod
    def yaml_load(stream):
        f = open(stream, encoding='utf-8')
        data = yaml.load(f)
        f.close()

        return data

    @staticmethod
    def yaml_dump(stream, data):
        f1 = open(stream, 'w', encoding='utf-8')
        yaml.dump(data, f1, allow_unicode=True)
        f1.close()

    @staticmethod
    def duplicate_removal(duplicate_list):
        """
        去重，lambda的意思是：if y 存在于 list x 中，return x, 否则
        返回x + [y]
        :param duplicate_list: 要去重的list
        :return:
        """
        from functools import reduce
        func = lambda x, y: x if y in x else x + [y]
        duplicate_list = reduce(func, [[], ] + duplicate_list)

        return duplicate_list

    @staticmethod
    def num_to_eng(num):
        """
        阿拉伯数字转换英文
        :param num: 数字
        :return: 英文
        """
        if 0 < num < 10:
            return {1: 'one', 2: 'two', 3: 'three', 4: 'four',
                    5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}[num]
        else:
            raise ValueError("value error, num is less than 10 and more than 0")

    @staticmethod
    def ndp_file_cutting(stream_in):
        dir_path = os.path.split(__file__)[0]
        ndp_file = Tools.yaml_load(stream_in)
        length = len(ndp_file)

        if 0 <= length <= 600:
            pass
        else:
            if length % 500 >= 100:
                files = int(math.ceil(length / 500))
            else:
                files = int(math.floor(length / 500))

            for i in range(files):
                data = []
                k = i * 500
                if i != (files - 1):
                    for j in range(k, k + 500):
                        data.append(ndp_file[j])
                else:
                    for j in range(k, length):
                        data.append(ndp_file[j])
                Tools.yaml_dump(dir_path.replace('tools', 'data\\ud_ndp\\ud_ndp_%s.yaml' % str(i + 1)), data)

    @staticmethod
    def thread_bar(index, length, c, _position, stream_out):
        k = index + 1
        thread_bar = '#' * ((k * 50) // length) + '-' * (50 - (k * 50) // length)
        info = '|(%s/%s), [%.2f%%]' % (k, length, float(k / length * 100))
        c.addstr(_position, 1, stream_out + ':|' + thread_bar + info)
        c.refresh()
